package vip.xiaonuo.modular.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.context.login.LoginContextHolder;
import vip.xiaonuo.core.exception.AuthException;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.pojo.login.SysLoginUser;
import vip.xiaonuo.modular.business.entity.BiUserIndexColor;
import vip.xiaonuo.modular.business.mapper.BiUserIndexColorMapper;
import vip.xiaonuo.modular.business.service.UserIndexColorService;
import vip.xiaonuo.modular.manage.biindextype.entity.BiIndexType;
import vip.xiaonuo.modular.manage.biindextype.service.BiIndexTypeService;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Author: liu
 * @Date:2022/9/2
 */
@Service
public class UserIndexColorServiceImpl implements UserIndexColorService {
    @Resource
    private BiUserIndexColorMapper biUserIndexColorMapper;

    @Resource
    private BiIndexTypeService biIndexTypeService;

    private HashMap<String,BiUserIndexColor> cacheMap = new HashMap<>();

    @Override
    public void updateColor(BiUserIndexColor param) {
        if (param.getIndexTypeId() == null ||
                StringUtils.isEmpty(param.getColor())) {
            throw new ServiceException(9, "参数错误");
        }
        SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
        String key = ""+param.getIndexTypeId()+sysLoginUser.getId();
        LambdaQueryWrapper<BiUserIndexColor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BiUserIndexColor::getUserId, sysLoginUser.getId());
        wrapper.eq(BiUserIndexColor::getIndexTypeId, param.getIndexTypeId());
        wrapper.last(" limit 1 ");
        BiUserIndexColor biUserIndexColor = biUserIndexColorMapper.selectOne(wrapper);
        if (biUserIndexColor == null) {
            param.setUserId(sysLoginUser.getId());
            biUserIndexColorMapper.insert(param);
            cacheMap.put(key,param);
        }else{
            biUserIndexColor.setColor(param.getColor());
            if (StringUtils.isNotEmpty(param.getIco())) {
                biUserIndexColor.setIco(param.getIco());
            }
            biUserIndexColorMapper.updateById(biUserIndexColor);
            cacheMap.put(key,param);
        }
    }

    @Override
    public BiUserIndexColor getBiIndexColor(BiUserIndexColor param) {
        if (param.getIndexTypeId() == null) {
            throw new ServiceException(9, "参数错误");
        }
        BiUserIndexColor object = select(param);
        if (object != null) {
            return object;
        }
        BiIndexType indexType = biIndexTypeService.getById(param.getIndexTypeId());
        if (indexType != null && StringUtils.isNotEmpty(indexType.getColor())) {
            BiUserIndexColor color = new BiUserIndexColor();
            color.setColor(indexType.getColor());
            return color;
        }
        BiUserIndexColor defaultColor = new BiUserIndexColor();
        defaultColor.setColor("#00FF00");
        return defaultColor;
    }

    private BiUserIndexColor select(BiUserIndexColor param){
        try {
            SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
            String key = ""+param.getIndexTypeId()+sysLoginUser.getId();
            if(cacheMap.containsKey(key)){
                return cacheMap.get(key);
            }
            LambdaQueryWrapper<BiUserIndexColor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BiUserIndexColor::getUserId, sysLoginUser.getId());
            wrapper.eq(BiUserIndexColor::getIndexTypeId, param.getIndexTypeId());
            wrapper.last(" limit 1 ");
            BiUserIndexColor biUserIndexColor = biUserIndexColorMapper.selectOne(wrapper);
            cacheMap.put(key,biUserIndexColor);
            return biUserIndexColor;
        } catch (AuthException e) {
        }
        return null;
    }

    @Override
    public String getIndexColor(long indexTypeId){
        BiUserIndexColor c = new BiUserIndexColor();
        c.setIndexTypeId(indexTypeId);
        return this.getBiIndexColor(c).getColor();
    }

    @Override
    public String getIndexColorIco(long indexTypeId){
        BiUserIndexColor param = new BiUserIndexColor();
        param.setIndexTypeId(indexTypeId);
        BiUserIndexColor object = select(param);
        if (object != null) {
            return object.getIco();
        }
        return "";
    }
}
